package com.wing.user.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;

import com.wing.user.model.entity.SystemDingTalkInfo;
import com.wing.user.model.request.SystemDingTalkInfoForm;
import com.wing.user.service.SystemDingTalkInfoService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author1
 */
@Validated
@Api(value = "Admin-钉钉配置接口表管理", tags = "#/system/systemDingTalkInfo")
@RestController("SystemDingTalkInfoController")
@RequestMapping("user/admin/systemDingTalkInfo")
@RequiredArgsConstructor
public class SystemDingTalkInfoController {

    private final SystemDingTalkInfoService systemDingTalkInfoService;


    @ApiOperation(value = "添加钉钉接口表", notes = "添加/修改")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/addOrUpdate")
    public JsonResult addOrUpdate(@RequestBody SystemDingTalkInfoForm systemNoteInfoForm) {

        if (systemDingTalkInfoService.addOrUpdate(systemNoteInfoForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }



    @ApiOperation(value = "查询所有钉钉接口表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<SystemDingTalkInfo> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
    ){

        Page<SystemDingTalkInfo> pg = new Page<>(page, limit);
        QueryWrapper<SystemDingTalkInfo> queryWrapper = new QueryWrapper<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            if (StringUtils.equals("asc", order)) {
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(sort));
            } else {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(sort));
            }
        }

        //搜索
        IPage<SystemDingTalkInfo> list = systemDingTalkInfoService.getBaseMapper().selectPage(pg, queryWrapper);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "获取钉钉配置信息", notes = "查询")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @GetMapping("/selDingTalkInfo")
    public JsonResult selDingTalkInfo() {
        return JsonResult.success(systemDingTalkInfoService.selDingTalkInfo());
    }


}
